﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.ObjectBuilder;
using NetDon.OA.Business;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.Linq;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Admin
{
    public partial class DeliveryOrderCodeEdit : PageBase
    {

        public Guid DeliveryOrderCodeConfigId
        {
            get { return Parameters.GetGuid("OID"); }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CancelButton = btnCancel;
                if (DeliveryOrderCodeConfigId != Guid.Empty)
                {
                    var entity = new DeliveryOrderCodeConfigEntity(DeliveryOrderCodeConfigId);
                    formCurrency.DataSource = entity;
                    formCurrency.DataBind();
                    this.ddlDeliveryCodeType.SelectedValue = entity.DeliveryCodeType.ToString();
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var metaData = new LinqMetaData();
            var count = (from entity in metaData.DeliveryOrderCodeConfig
                         where entity.DeliveryCodeType == ddlDeliveryCodeType.SelectedValue.ToGuid()
                         select entity.Id).Count();
            bool duplciateNameExists = DeliveryOrderCodeConfigId == Guid.Empty ? count > 0 : count > 1;
            if (duplciateNameExists)
            {
                Alert("此交货单类型配置已存在，请重新选择.");
                return;
            }
            DeliveryOrderCodeConfigEntity deliveryOrderCodeConfigEntity;
            if (DeliveryOrderCodeConfigId == Guid.Empty)
            {
                deliveryOrderCodeConfigEntity = new DeliveryOrderCodeConfigEntity();
                deliveryOrderCodeConfigEntity.Id = Guid.NewGuid();
            }
            else
            {
                deliveryOrderCodeConfigEntity = new DeliveryOrderCodeConfigEntity(DeliveryOrderCodeConfigId);
            }
            deliveryOrderCodeConfigEntity.DeliveryCodeType = ddlDeliveryCodeType.SelectedValue.ToGuid();
            deliveryOrderCodeConfigEntity.DeliveryOrderCodePrefix = this.txtDeliveryOrderCodePrefix.Text.Trim();
            deliveryOrderCodeConfigEntity.AreaId = CurrentApplicationContext.CompanyId;
            if (deliveryOrderCodeConfigEntity.Save())
            {
                this.CloseWindow(true);
            }

        }


    }
}